package com.liu.project.controller;

import com.liu.project.pojo.Log;
import com.liu.project.pojo.LoginUser;
import com.liu.project.service.LogService;
import com.liu.project.utils.Message;
import com.liu.project.utils.UserUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: project
 * @description: 用于行为
 * @author: 86187
 * @create: 2022-03-26 17:26
 **/

@Api(tags = "用户行为数据添加")
@RestController
@RequestMapping("/api/log/")
public class LogController {

    @Autowired
    LogService logService;

    @ApiOperation("添加行为日志")
    @GetMapping("/add")
   @ApiResponses({@ApiResponse(code = 200,message ="请求成功" ,response = Message.class),
           @ApiResponse(code = 404,message ="请求失败" ,response = Message.class)})
    public Message add(Log log){
        boolean b = logService.addLog(log);
        if (b)return new Message(200,"请求成功");
        else  return new Message(404,"请求失败");
    }

    @ApiOperation("通过id删除行为数据")
    @ApiImplicitParam(name = "id",value = "行为id")
    @ApiResponses({@ApiResponse(code = 200,message ="请求成功" ,response = Message.class),
            @ApiResponse(code = 404,message ="请求失败" ,response = Message.class)})
    @GetMapping("/delete/{id}")
    public Message delete(@PathVariable("id") int id){
        boolean b = logService.deleteLog(id);
        if (b)return new Message(200,"请求成功");
        else  return new Message(404,"请求失败");
    }
    @ApiOperation("当前登录用户的行为日志")
    @ApiResponse(code = 200,message ="请求成功" ,response = Message.class)
    @PostMapping("/getlog")
    @ResponseBody
    public Message getLogByUserId(){
      int userId = UserUtil.getLoginUser().getId();
        List<Log> logList = logService.getLogs(userId);
        return new Message(200,"请求成功",logList);
    }

}
